home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / MouseTrack.java < prev    next >
Text File  |  1998-09-15  |  5KB  |  141 lines

  1. /*
  2.  * @(#)MouseTrack.java    1.6 98/03/23
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.event.*;
  32. import java.awt.Graphics;
  33. import java.lang.Math;
  34.  
  35. public class MouseTrack extends java.applet.Applet implements MouseListener, MouseMotionListener {
  36.  
  37.     int mx, my;
  38.     int onaroll;
  39.  
  40.     public void init() {
  41.     onaroll = 0;
  42.     setSize(500, 500);
  43.     addMouseListener(this);
  44.     addMouseMotionListener(this);
  45.     }
  46.  
  47.     public void destroy() {
  48.         removeMouseListener(this);
  49.         removeMouseMotionListener(this);
  50.     }
  51.  
  52.     public void paint(Graphics g) {
  53.     g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
  54.     mx = (int)(Math.random()*1000) % (getSize().width - (getSize().width/10));
  55.     my = (int)(Math.random()*1000) % (getSize().height - (getSize().height/10));
  56.     g.drawRect(mx, my, (getSize().width/10) - 1, (getSize().height/10) - 1);
  57.     }
  58.  
  59.     /*
  60.      * Mouse methods
  61.      */
  62.     public void mouseDragged(MouseEvent e) {
  63.     }
  64.  
  65.     public void mouseMoved(MouseEvent e) {
  66.         e.consume();
  67.         if((e.getX() % 3 == 0) && (e.getY() % 3 == 0))
  68.             repaint();
  69.     }
  70.  
  71.     public void mousePressed(MouseEvent e) {
  72.         int x = e.getX();
  73.         int y = e.getY();
  74.         e.consume();
  75.         requestFocus();
  76.         if((mx < x && x < mx+getSize().width/10-1) && (my < y && y < my+getSize().height/10-1)) {
  77.             if(onaroll > 0) {
  78.                 switch(onaroll%4) {
  79.                     case 0:
  80.                         play(getCodeBase(), "sounds/tiptoe.thru.the.tulips.au");
  81.                         break;
  82.                     case 1:
  83.                         play(getCodeBase(), "sounds/danger,danger...!.au");
  84.                         break;
  85.                     case 2:
  86.                         play(getCodeBase(), "sounds/adapt-or-die.au");
  87.                         break;
  88.                     case 3:
  89.                         play(getCodeBase(), "sounds/cannot.be.completed.au");
  90.                         break;
  91.                 }
  92.                 onaroll++;
  93.                 if(onaroll > 5)
  94.                     getAppletContext().showStatus("You're on your way to THE HALL OF FAME:"
  95. + onaroll + "Hits!");
  96.                 else
  97.                     getAppletContext().showStatus("YOU'RE ON A ROLL:" + onaroll + "Hits!");
  98.             }
  99.             else {
  100.                 getAppletContext().showStatus("HIT IT AGAIN! AGAIN!");
  101.                 play(getCodeBase(), "sounds/that.hurts.au");
  102.                 onaroll = 1;
  103.             }
  104.         }
  105.         else {
  106.             getAppletContext().showStatus("You hit nothing at (" + x + ", " + y + "), exactly\n");
  107.             play(getCodeBase(), "sounds/thin.bell.au");
  108.             onaroll = 0;
  109.         }
  110.         repaint();
  111.     }
  112.  
  113.     public void mouseReleased(MouseEvent e) {
  114.     }
  115.  
  116.     public void mouseEntered(MouseEvent e) {
  117.         repaint();
  118.     }
  119.  
  120.     public void mouseExited(MouseEvent e) {
  121.         onaroll = 0;
  122.         repaint();
  123.     }
  124.  
  125.     public void mouseClicked(MouseEvent e) {
  126.     }
  127.  
  128.     /**
  129.      * Focus methods
  130.      */
  131.     public void keyDown(int key) {
  132.     requestFocus();
  133.     onaroll = 0;
  134.     play(getCodeBase(), "sounds/ip.au");
  135.     }
  136.  
  137.     public String getAppletInfo() {
  138.         return "Title: JumpingBox\nAuthor: Anonymous";
  139.     }
  140. }
  141.